<?php

namespace app\index\controller;

use think\Controller;
use think\Paginator;


class Index extends Controller
{
    public function index()
    {

        //return '<style type="text/css">*{ padding: 0; margin: 0; } .think_default_text{ padding: 4px 48px;} a{color:#2E5CD5;cursor: pointer;text-decoration: none} a:hover{text-decoration:underline; } body{ background: #fff; font-family: "Century Gothic","Microsoft yahei"; color: #333;font-size:18px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.6em; font-size: 42px }</style><div style="padding: 24px 48px;"> <h1>:)</h1><p> ThinkPHP V5<br/><span style="font-size:30px">十年磨一剑 - 为API开发设计的高性能框架</span></p><span style="font-size:22px;">[ V5.0 版本由 <a href="http://www.qiniu.com" target="qiniu">七牛云</a> 独家赞助发布 ]</span></div><script type="text/javascript" src="https://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script><script type="text/javascript" src="https://e.topthink.com/Public/static/client.js"></script><think id="ad_bd568ce7058a1091"></think>';
        $data['user_ip'] = $this->getip();
        $data['login_time']=date('Y-m-d h:i:s',time());
        db('user_ip')->insert($data);
        return $this->fetch();
    }

    public function get_out_put($stdin = "")
    {
        $this->fromurl();
        $file = "./static/test/error.txt";
        if (filesize($file) > 0) {
            $errorfile = fopen($file, "r") or die("Unable to open file!");
            $message = fread($errorfile, filesize($file));
            echo($message);
            fclose($errorfile);
        } else {
            $stdin = explode("\n", $stdin);
            $command = './a.out';
            for ($i = 0; $i < count($stdin); $i++) {
                $command .= ' ' . $stdin[$i];
            }
            system($command, $result);
          // dump($result[0]);
            $code = "killall a.out";
            system($code);
        }
    }

    public function run_bat()
    {
        $this->fromurl();
        $shell = "gcc ./static/test/test.c 2> ./static/test/error.txt;";
        system($shell, $status);
        if ($status) {
            return true;
        } else {
            return false;
        }
    }


    public function compile()
    {
        $this->fromurl();
        //  $input['code']= "sds";
        //   $input['std'] = "ds0";
        // $input ='{"lengh":7,"code":"I2luY2x1ZGUgPHN0ZGlvLmg+DQppbnQgbWFpbigpDQp7DQogICAvLyBwcmludGYoKSDkuK3lrZfnrKbkuLLpnIDopoHlvJXlj7cNCiAgIHByaW50ZigiSGVsbG8sIGxkISIpOw0KICAgcmV0dXJuIDA7DQp9","stdin":""}';
        $code = "killall a.out";
        system($code);
        $input = file_get_contents("php://input"); //接收POST数据
        $input = json_decode($input, true);
        $code = $input['code'];
        $stdin = $input['stdin'];
        //return($code);
        $code = base64_decode($code);
        $this->file_input($code);
        if ($input['option'] == 1) {
            $this->run_bat();
            $this->get_out_put($stdin);
        } else {
           $this->download();
        }
        $this->user_code_rewrite($code);
    }

    public function file_input($input)
    {   $this->fromurl();
        $myfile = fopen("./static/test/test.c", "w") or die("Unable to open file!");
        fwrite($myfile, $input);
        fclose($myfile);
    }

    public function download()
    { $this->fromurl();
        $shell = "gcc static/test/test.c  -o ./static/test/test.exe";
        system($shell, $status);
        if ($status) {
            echo("编译失败");
        } else {
            $file_url ="./static/test/test.exe";
            $new_name ="3003.exe";
            if(!isset($file_url)||trim($file_url)==''){
                echo '500';
            }
            if(!file_exists($file_url)){ //检查文件是否存在
                echo '404';
            }
            $file_name=basename($file_url);
            $file_type=explode('.',$file_url);
            $file_type=$file_type[count($file_type)-1];
            $file_name=trim($new_name)?$file_name:urlencode($new_name);
            $file_type=fopen($file_url,'r'); //打开文件
            //输入文件标签
            header("Content-type: application/octet-stream");
            header("Accept-Ranges: bytes");
            header("Accept-Length: ".filesize($file_url));
            header("Content-Disposition: attachment; filename=".$file_name);
            //输出文件内容
            echo fread($file_type,filesize($file_url));
            fclose($file_type);
        }
    }
    public function user_code_rewrite($code){
        $this->fromurl();
        $time = date('Ymdhis',time());
        $path = "./static/uscode/C".$time.".txt";
        //dump($path);
        $wf = fopen($path, "w") or die("Unable to open file!");
        fwrite($wf, $code);
        fclose($wf);
        $message = array(
            "user_IP"=>$this->getip(),
            "user_code"=>$path,
            "use_time" =>date('Y-m-d h:i:s',time())
        );
        db('user_code')->insert($message);

    }
    static public function getip()
    {

        if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown"))
        {
            $ip = getenv("HTTP_CLIENT_IP");
        }
        else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown"))
        {
            $ip = getenv("HTTP_X_FORWARDED_FOR");
        }
        else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown"))
        {
            $ip = getenv("REMOTE_ADDR");
        }
        else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown"))
        {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        else
        {
            $ip = "";
        }
        return($ip);
    }
 public function fromurl(){
     if(!(isset($_SERVER['HTTP_REFERER'] )))
     {
         header("Location:http://118.25.109.192/");
         exit;
     }
 }
}